package com.ctsi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctsi.common.util.Search;
import com.ctsi.core.common.constant.MarsConstant;
import com.ctsi.core.common.exception.MarsException;
import com.ctsi.core.common.util.$;
import com.ctsi.core.database.util.PageUtil;
import com.ctsi.system.entity.Dict;
import com.ctsi.system.enums.SystemExceptionEnum;
import com.ctsi.system.mapper.DictMapper;
import com.ctsi.system.service.IDictService;
import com.ctsi.system.vo.DictResVO;
import com.ctsi.system.vo.DictTreeResVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author wang xiao xiang
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

	@Override
	public IPage<Dict> listPage(Search search) {
		LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
		boolean isKeyword = $.isNotBlank(search.getKeyword());
		if ($.isNotEmpty(search.getId())) {
			queryWrapper.eq(Dict::getParentId, search.getId());
		} else {
			queryWrapper.eq(Dict::getParentId, 0);
		}
		if (isKeyword) {
			queryWrapper.like(isKeyword, Dict::getDictValue, search.getKeyword())
					.or(isKeyword)
					.like(isKeyword, Dict::getDictKey, search.getKeyword());
		}
		queryWrapper.orderByDesc(Dict::getCreateTime);
		return this.page(PageUtil.getPage(search), queryWrapper);
	}

	@Override
	public String getValue(String code, String dictKey) {
		return this.getOne(Wrappers.<Dict>query().lambda()
				// 根据编码和键进行查询
				.eq(Dict::getCode, code)
				.eq(Dict::getDictKey, dictKey))
				.getDictValue();
	}

	@Override
	public List<Dict> getList(String code) {
		// 根据编码查询列表
		return this.list(Wrappers.<Dict>query().lambda()
				.eq(Dict::getCode, code));
	}

	@Override
	public List<DictResVO> getDictSubList(String code) {
		return this.list(Wrappers.<Dict>query().lambda()
				.eq(Dict::getCode, code)
				.ne(Dict::getDictKey, -1))
				.stream().map(sysDict -> DictResVO.builder()
						.code(sysDict.getDictKey())
						.value(sysDict.getDictValue()).build())
				.collect(Collectors.toList());
	}

	@Override
	public List<DictTreeResVO> tree() {
		// 查询所以字典列表
		List<Dict> list = this.list(Wrappers.<Dict>lambdaQuery().eq(Dict::getStatus, 0));
		// 转换为字典树响应对象返回
		return list.stream().map(dict -> $.copy(dict, DictTreeResVO.class)).collect(Collectors.toList());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean delDict(List<Long> ids) {
		ids.forEach(id -> {
			Dict dict = this.getById(id);
			if (dict.getReadable() == 0) {
				throw new MarsException(SystemExceptionEnum.DICT_READ_ONLY);
			}
			if ($.equals(MarsConstant.TREE_ROOT.toString(), dict.getDictKey())) {
				if (this.list(Wrappers.<Dict>lambdaQuery().eq(Dict::getParentId, id)).size() > 0) {
					throw new MarsException(SystemExceptionEnum.DICT_VALUE_NOT_NULL);
				}
			}
			this.removeById(id);
		});
		return Boolean.TRUE;
	}
}
